<?php
define('o', 0); //声明可以访问 
// 载入初始化且必须在任何输出之前包括HTML标签
require_once('./include/initialize.php');
if(!empty($_REQUEST)){
	if(!empty($_REQUEST['control'])){
		switch ($_REQUEST['control']) {
			case 'login':
				session_start();
				if(empty($_SESSION['time']))
				{
					$_SESSION['email'] = $_REQUEST['email'];
					$temp = new user("xxx");
					if($temp->login($_REQUEST['email'],$_REQUEST['password'],$id,$name,$class_id) > 0 ){
						$_SESSION['id'] = $id;
						$_SESSION['name'] = $name;
						$_SESSION['time'] = time();
						$_SESSION['class'] = $class_id;
						$temp->close();
						setcookie('PHPSESSID',session_id(),time()+3600*3);
						if (!empty($_COOKIE['src'])) {
							header("Location:" . $_COOKIE['src']);
							die;
						}else{
							header("Location:./");
							die;
						}
					}else{
						$error = "登陆失败，账户或密码错误！";
						include(ROOT . "/error.php");
					}
				}else{
					if (!empty($_COOKIE['src'])) {
							header("Location:" . $_COOKIE['src']);
							die;
						}else {
							header("Location:./index.php");
							die;
						}
				}
				break;
			case 'quit':
				session_start();
				if(!empty($_SESSION['time'])){
					unset($_SESSION['time']);
					unset($_SESSION['id']);
					unset($_SESSION['name']);
					unset($_SESSION['class']);
					$url = '.';
					$error = "成功退出！";
					include(ROOT . "/error.php");
				}else{
					header("Location:./login.php");
					die;
				}
				break;
			case 'augment':
				session_start();
				if(!empty($_SESSION['time'])){
					// print_r($_REQUEST);
					if (empty($_SESSION['times']) && !empty($_REQUEST['times'])) {
						$_SESSION['times'] = $_REQUEST['times'];
						$editlog = new editlog('xxx_editlog');
						if( $editlog->selEditlog(array(
								'key_id'=>$_REQUEST['type'],
								'editlog_operate'=>$_REQUEST['operate'],
								'editlog_text'=>$_REQUEST['text'],
								'goods_id'=>$_REQUEST['goodsid']
							))){
							$rel = $editlog->edit($_REQUEST);
							$editlog->close();
							if (empty($rel)) {
								header("Location:./augment.php?id=" . $_REQUEST['goodsid']);
							}else{
								$error = $rel;
								include(ROOT . "/error.php");
							}
							
					}else{
						// echo "重复内容提交";
						$error = "重复内容提交";
						include(ROOT . "/error.php");
					}
						
					}elseif (!empty($_SESSION['times']) && !empty($_REQUEST['times']) && $_SESSION['times']!=$_REQUEST['times']) {
						$_SESSION['times'] = $_REQUEST['times'];
						$editlog = new editlog('xxx_editlog');
						if($editlog->selEditlog(array(
								'key_id'=>$_REQUEST['type'],
								'editlog_operate'=>$_REQUEST['operate'],
								'editlog_text'=>$_REQUEST['text'],
								'goods_id'=>$_REQUEST['goodsid']
							))){
							$rel = $editlog->edit($_REQUEST);
							$editlog->close();
							if (empty($rel)) {
								header("Location:./augment.php?id=" . $_REQUEST['goodsid']);
							}else{
								$error = $rel;
								include(ROOT . "/error.php");
							}
						}else{
							$error = "重复内容提交";
							include(ROOT . "/error.php");
							
						}						

					}else{
						// echo "请勿刷新重复提交";
						$error = "请勿刷新重复提交";
						include(ROOT . "/error.php");
					}
					
				}else{
					header("Location:./login.php");
				}				
				break;
			case 'assess':
				session_start();
				if(!empty($_SESSION['time'])){
					$assess = new assess('xxx_assess');
					$rel = $assess->querydata(array('assess_id'),'where message_id='.$_REQUEST['messageid'].' and ' .'user_id='.$_SESSION['id'] .' and type_id='.$_REQUEST['typeid']);
					if (empty($rel)) {
						$assess->addassess($_REQUEST['messageid'],$_REQUEST['typeid'],$_SESSION['id'],$_REQUEST['goodbay']);
						$assess->close();
						echo "感谢评价";
						die;
					}		
					echo "false";
					$assess->close();
					die;
				}else{
					echo "false";
				}
				break;
			case 'register':
				session_start();
				if(!empty($_SESSION['time'])){
					$url = '.';
					$error = "你已经登录了！";
					include(ROOT . "/error.php");
				}else{
					if (count($_REQUEST)>=3) {
						$user = new user('xxx_user');
						$rel = $user->querydata(array('user_id'),' where user_email=\''.$_REQUEST['email'].'\'');
						if (empty($rel)) {
							$user->create(array('email'=>$_REQUEST['email'],'password'=>$_REQUEST['password']));
							$user->close();
							$url = './login.php';
							$error = "注册成功，欢迎你的加入！";
							include(ROOT . "/error.php");
						}else{
							$url = './register.php';
							$error = "email已经被注册了！";
							include(ROOT . "/error.php");
						}

					}else{
						$url = './register.php';
						$error = "参数错误!".count($_REQUEST);
						include(ROOT . "/error.php");
					}
				}
				break;
			case 'examine':
					session_start();
					if(!empty($_SESSION['time'])){
						//examineval
						if (count($_REQUEST)>=3) {
							$editlog = new editlog('xxx_editlog');
							$editlogval = $editlog->getEditlog($_REQUEST['messageid']);
							if (empty($editlogval)) {
								$editlog->close();
								echo "错误";
								die;
							}else{
								$message = new message('xxx_message');
								$messageval =$message->queryid($editlogval['goods_id'],$editlogval['key_id']);
								if (empty($messageval)) {
									if ($editlogval['editlog_operate'] == 'add' && $_REQUEST['examineval']==1) {
										$message->addmessage(array(
										'goods_id'=>$editlogval['goods_id'],
										'type_id' =>$editlogval['key_id'],
										'message_val' =>$editlogval['editlog_text']
										));
										$editlog->assess($_REQUEST['messageid'],1,$_SESSION['id']);
										$editlog->close();
										$message->close();
										goods::unsetfile($editlogval['goods_id']);
										echo "renovate";
										die;
									}else{
										$editlog->assess($_REQUEST['messageid'],2,$_SESSION['id']);
										$editlog->close();
										$message->close();
									}
									goods::unsetfile($editlogval['goods_id']);
									echo "renovate";
									die;
								}

								if (!empty($_REQUEST['examineval']) && $_REQUEST['examineval']!= 1) {
									$editlog->assess($_REQUEST['messageid'],2,$_SESSION['id']);
									$editlog->close();
									goods::unsetfile($editlogval['goods_id']);
									echo "renovate";
									die;
								}else{
									if (!empty($editlogval['editlog_operate'])) {

										if ($editlogval['editlog_operate'] == 'add') {
											$message->upmessage(
												$messageval['message_id'],
												$messageval['message_val'].' '.$editlogval['editlog_text']);
											$editlog->assess($_REQUEST['messageid'],1,$_SESSION['id']);
											goods::unsetfile($editlogval['goods_id']);
											echo "renovate";
											die;

										}else if ($editlogval['editlog_operate'] == 'update') {
											$message->upmessage(
												$messageval['message_id'],
												str_ireplace($editlogval['editlog_usercontent'],$editlogval['editlog_text'],$messageval['message_val'])
												);
											$editlog->assess($_REQUEST['messageid'],1,$_SESSION['id']);
											goods::unsetfile($editlogval['goods_id']);
											echo "renovate";

										}else if ($editlogval['editlog_operate'] == 'delete') {

											$message->upmessage(
												$messageval['message_id'],
												str_ireplace($editlogval['editlog_text'],'',$messageval['message_val'])
												);
											$editlog->assess($_REQUEST['messageid'],1,$_SESSION['id']);
											goods::unsetfile($editlogval['goods_id']);
											echo "renovate";
										}else{
											echo "error";
										}
									}
								}
								

								$message->close();
								$editlog->close();
								die;
							}
							
						}
					}else{
						$url = './login.php';
						$error = "你还没有登录！";
						include(ROOT . "/error.php");
					}
				break;
			case 'addgoods':
				session_start();
				if(!empty($_SESSION['time'])){
					if (!empty($_REQUEST['p'])) {
						if (!empty($_REQUEST['pv'])) {
							$goodsx = new goodsx('xxx_goodsx');
							$rel=$goodsx->selectdata($_REQUEST['p'],$_REQUEST['pv']);
							if (!empty($rel)) {
								$goodsx->close();
								$url = './add.php';
								$error = "已经有人提交了！";
								include(ROOT . "/error.php");
							}else{
								$goodsx->addgoods($_REQUEST['pv'],$_REQUEST['p'],7,$_SESSION['id']);
								$goodsx->close();
								$url = './add.php';
								$error = "你提交的数据已经进入审核流程，感谢你帮助我们完善!";
								include(ROOT . "/error.php");
							}
							return true;
						}
					}
					$url = './add.php';
					$error = "信息提供不完整，请重新输入。";
					include(ROOT . "/error.php");

				}else{
					$url = './login.php';
					$error = "你还没有登录！";
					include(ROOT . "/error.php");
				}
			break;
			case 'upfile':
				$file = $_FILES['files']['tmp_name'];
				$wb = md5($file);
					$bool = copy($_FILES['files']['tmp_name'],ROOT .'/temp/'.$wb);
					$handle = fopen(ROOT .'/temp/'.$wb,'r');
					fgetcsv($handle);
					while ($data = fgetcsv($handle)) {
						$goods = new goods('xxx_goods');						
						$goods->addgoods($data);
						$goods->close();
					}
					fclose($handle);
					unlink(ROOT .'/temp/'.$wb);
					

				if ($bool) {
					$error = "成功提交数据。";
					include(ROOT . "/error.php");
					break;
				}else{
					$error = "提交数据失败。";
					include(ROOT . "/error.php");
					break;
				}
				
			default:
				$error = "illegal request!";
				include(ROOT . "/error.php");
				break;
		}
	}else{
		$error = "illegal request!";
		include(ROOT . "/error.php");
		die;
	}    
}else{
	$error = "illegal request!";
	include(ROOT . "/error.php");
	die;
}    


?>